package cn.qyl.joha.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.qyl.joha.common.PageConstant;
import cn.qyl.joha.common.PageModel;
import cn.qyl.joha.dto.Picturcarousel;
import cn.qyl.joha.service.PicService;
import cn.qyl.joha.service.Impl.PicServiceImpl;
import cn.qyl.joha.utils.FilePath;
import cn.qyl.joha.utils.UpLoadUtils;
/**
 * 图片轮播控制层
 * @author Administrator
 *
 */



@Controller
@RequestMapping("admin/pic")
public class PicController {
	PicService picService = new PicServiceImpl();
	
	@RequestMapping("getPicList")
	//图片轮播分页
	public String getPicList(ModelMap map ,String pageIndex,Picturcarousel pic){
		
		PageModel pageModel = new PageModel();
		
		if(pageIndex!= null && pageIndex!=""){
			pageModel.setPageIndex(Integer.parseInt(pageIndex));
		}else{
			pageModel.setPageIndex(0);
		}
		
		int count = picService.getCountByPic(pic);
		
		pageModel.setRecordCount(count);
		
		List<Picturcarousel> pics = picService.getPicList(pageModel);
		
		map.addAttribute("pics",pics);
		map.addAttribute("pageModel", pageModel);
		map.addAttribute("pic",pic);
		
		return "pic/picList";
	}
	//跳转新增图片页面
	@RequestMapping("toAddPic")
	public	String toAddPic(){
		return "pic/addPic";
	}
	//新增图片
	@RequestMapping("addPic")
	
	public String addPic(Picturcarousel pic,ModelMap map){
		
		picService.addPic(pic);
		
		map.addAttribute(PageConstant.TIP,"添加成功");
		
		return "pic/addPic";
		
	}
	//删除图片
	@RequestMapping("delPicById")
	public String delPicById(int pid ,ModelMap map){
		int count = picService.delPicById(pid);
		
		if(count>0){
			map.put(PageConstant.TIP, "删除成功");
		}else{
			map.put(PageConstant.TIP, "删除失败");
		}
		return "forward:getPicList";
	}
	//批量删除图片
	@RequestMapping("delPicByPids")
	public String delPicByPids(int[] pids,ModelMap map){
		
		int count = picService.delPicByPids(pids);
		
		if(count>0){
			map.put(PageConstant.TIP, "删除成功");
		}else{
			map.put(PageConstant.TIP, "删除失败");
		}
		return "forward:getPicList";
	}
	
	//跳转更新图片页面
	@RequestMapping("toPicUpdate")
	public String toPicUpdate(int id,ModelMap map){
		Picturcarousel  pic = picService.toPicUpdate(id);
		map.addAttribute("pic", pic);
		return "pic/updatePic";		
	}
	
	@RequestMapping("PicUpdate")
	public String PicUpdate(Picturcarousel pic,ModelMap map){
		
		int count = picService.PicUpdate(pic);
		
		if(count>0){
			map.put(PageConstant.TIP, "修改成功");
		}else{
			map.put(PageConstant.TIP, "修改失败");
		}
		
		return "pic/updatePic";
	}
	
	
	
	
	
	
	//异步获取图片
	@RequestMapping("/uploadImgAjax")
	@ResponseBody
	public Map<String,String> uploadImgAjax(MultipartFile myFile) {
		
		Map<String,String> map = new HashMap<>();
		String fileName = null;
		//保存文件
		try{
			fileName=UpLoadUtils.saveFile(myFile);
			map.put("status", "success");
			map.put("fileName", fileName);
		}catch(Exception ex){
			ex.printStackTrace();
			map.put("status", "fail");
		}
		return map;
		
	}
}


